package com.nacrtstudio.webgiftshop.pages;

import javax.inject.Inject;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.services.Request;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.subsystems.Mailing;

public class Contact{
	@Inject
	private Request request;
	public void onActivate() {
		try {
			if (controller.getRequest()==null) {
	    		controller.setRequest(request);
			}
		} catch (Exception e) {
		}
	}
	
	@Inject
	private Mailing mailing;
	@Inject
	private Controller controller;
	@InjectPage
	private StatusPage statusPage;
	@Component
    private Form mailForm;
	
	@Property
	private String mailFrom;
	@Property
	private String mailSubject;
	@Property
	private String mailMsg;
	
	Object onSubmitFromMailForm(){
		try {
			if (controller.getLoggedSignal()) {
				mailing.sendMail(controller.getLoggedUser().geteMail(), "batasone012@gmail.com", "Message from contact form - customer: "+controller.getLoggedUser().getFirstName(), "From: "+mailFrom+"\nSubject: "+mailSubject+"\nText: "+mailMsg);
			} else {
				mailing.sendMail("nacrtstudio", "batasone012@gmail.com", "Message from contact form - customer: guest", "From: "+mailFrom+"\nSubject: "+mailSubject+"\nText: \n\n"+mailMsg);
			}
			statusPage.setMessage("Uspesno poslat mail!");
			return statusPage;
		} catch (Exception e) {
			statusPage.setMessage(e.getMessage());
			return statusPage;
		}
	}
}
